package com.hnevc.foodserverapi.controller;


import com.hnevc.foodserverapi.pojo.Foods;
import com.hnevc.foodserverapi.service.FoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/foodserver/food")
public class FoodsController {
    @Autowired
    FoodsService foodsService;

    /**
     * 查询所有菜品API
     * 无需传参
     * 返回json对象，数据data
     */
    @PostMapping("/list")
    @ResponseBody
    public Map<String,Object> queryAllFoods(){
        Map<String,Object> map = new HashMap<>();
        List<Foods> list = foodsService.queryAllFoods();
//        如果没有查到记录就返回失败，查到返回成功
        if (list!=null){
            map.put("code",0);
            map.put("errMsg","成功");
            map.put("list",list);
        }else {
            map.put("code",1);
            map.put("errMsg","失败");
        }

        return map;
    }

    /**
     * 查询菜单详情API
     * 参数类型 int
     * 返回json对象，数据data
     */
    @GetMapping("/findone")
    @ResponseBody
    public Map<String, Object> queryDetails(int id) {
        Map<String, Object> map = new HashMap<>();
        Foods foods = foodsService.queryDetails(id);
        if (foods != null) {
            map.put("msg", "成功");
            map.put("data", foods);
            map.put("success", true);
        } else {
            map.put("success", false);
            map.put("msg", "失败");
        }
        //如果没有查到记录就返回失败，查到返回成功
        return map;
    }

    //将重复代码封装
    private Map<String, Object> getMap(Map<String, Object> map, List<Foods> list) {
        if (!list.isEmpty()){
            map.put("data",list);
            map.put("success",true);
            map.put("msg","成功");
        }else {
            map.put("success",false);
            map.put("msg","失败");
        }
        return map;
    }
}
